function Group() {
    this.grid = null;
    //this.InitGrid()
    this.table = this.InitTable();
    this.tab = this.InitTab();

    this.form = this.InitEleUI();


}

Group.prototype.InitEleUI = function () {
    var form;
    layui.use('form', function () {
        form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
        form.render();
    });
    return form;
}
Group.prototype.InitTab = function () {
    var element;
    layui.use('element', function () {
        element = layui.element;
    });
    return element;
}

Group.prototype.OpTab = function (tabindex, obj) {
    this.tab.tabChange('tabcontent', tabindex);
    if (obj) {
        document.formedit.groupname.value = obj.groupname;
        document.formedit.remark.value = obj.remark;
        document.formedit.groupid.value = obj.groupid;
        document.formedit.orderindex.value = obj.orderindex;
        this.form.render();
    }
}

Group.prototype.InitTable = function () {
    var table;
    var _this = this;
    layui.use('table', function () {
        table = layui.table;
        table.render({
            elem: '#list'
            , height: $(window).height() - 140
            , url: 'get_group_list' // 数据接口
            , page: true // 开启分页
            , limit: 30
            , id: 'grouplist'
            , method: "post"
            , cols: [[ // 表头
                {type: 'numbers'}
                , {field: 'groupname', title: '分组名称', width: 200, align: 'center'}
                //, {field : 'grouptype',title : '类型',width : 120,align : 'center'}
                //, {field : 'createtime',title : '时间',width : 170,align : 'center'}
                , {field: 'remark', title: '描述', width: 240, align: 'center'}
                , {title: '操作', width: 130, align: 'center', toolbar: '#operate'}
            ]]
            , request: {
                type: 'post'
            }
            , even: true //开启隔行背景
            , where: {groupname: $("#groupname").val()}
            , done: function (res, page, count) {
                $("[data-field='grouptype']").children().each(function () {
                    if ($(this).text() == '0') {
                        $(this).text("普通民警")
                    }
                    else if ($(this).text() == '1') {
                        $(this).text("部门管理员")
                    }
                    else if ($(this).text() == '2') {
                        $(this).text("系统管理员")
                    }
                })
            }

        });
        //监听工具条
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {

                top.mylayui.confirm('确定要删除吗？', {icon: 0}, function (index, dd) {
                    top.mylayui.close(index);
                    vOrgGroup.deleteentity(data.groupid);
                });
            } else if (obj.event === 'edit') {
                //layer.alert('编辑行：<br>'+ JSON.stringify(data))
                _this.OpTab(2, data);
            }
            else if (obj.event == 'setrights') {
                vOrgGroup.openadd(data.groupid);
            }
        });
    });

    return table;
}


Group.prototype.InitGrid = function () {
    var _this = this;
    window.liger.remove("list");
    if (_this.grid == null) {
        _this.grid = $("#list").ligerGrid({
            columns: [{display: '序号', width: 80, name: 'rowNum'},
                {display: '分组名称', width: 120, name: 'groupname'},
                {
                    display: '类型', width: 120, name: 'grouptype', render: function (rowdata, rowindex, value) {
                        if (value == 1) {
                            return "系统分组";
                        }
                        else {
                            return "项目分组";
                        }
                    }
                },
                //{ display: '从属项目', width: 150, name: 'projectname' },
                {display: '描述', width: 200, name: 'remark'},
                {
                    display: '创建时间', width: 130, name: 'createtime', render: function (rowdata, rowindex, value) {
                        return value;
                    }
                },
                {
                    display: '操作', width: 270, name: 'userid', render: function (rowdata, rowindex, value) {
                        return '';
                    }
                }
            ],
            pageSize: 15,
            url: "get_list" + '?time=' + new Date().getTime(),
            delayLoad: false,            //初始化时是否加载,为true时,不加载,为false时,则加载
            sortName: null,
            dataAction: 'server',
            parms: {
                Method: "",
                groupname: $("#groupname").val()
            },
            sortOrder: null,
            sourceObj: _this.grid,
            root: 'items',                       //数据源字段名
            record: 'countRecord',                    //数据源记录数字段名
            pageParmName: 'CurrentPage',               //页索引参数名，(提交给服务器)
            pagesizeParmName: 'PageSize',        //页记录数参数名，(提交给服务器)
            data: {},
            width: "100%",
            height: '100%',
            heightDiff: -120,
            onSuccess: function (rowdata, rowindex) {
                if (rowdata.status > 0) {
                    alert(rowdata.msg)
                }
            },
            alternatingRow: true,
            border: 0
        })
    }
}

Group.prototype.searchlist = function () {
    this.grid.setOptions({
        newPage: 1, parms: {
            Method: "",
            groupname: $("#groupname").val()
        }
    });
    this.grid.loadData();
}

Group.prototype.searchtable = function () {
    this.table.reload('grouplist', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        , where: {
            groupname: $("#groupname").val()
        }
    });
}

Group.prototype.deleteentity = function (groupid) {
    $.ajax({
        type: "POST",
        url: "delete_group",
        data: {groupid: groupid},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                top.mylayui.msg(data.msg, {icon: 1});
                vOrgGroup.searchtable();
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}


Group.prototype.openadd = function (groupid) {
    top.mylayui.open({
        type: 2,
        title: '分组设置',
        shadeClose: true,
        shade: 0.5,
        area: ['800px', '500px'],
        content: 'workass/work_rights?groupid=' + groupid
    });

    var iframeele = top.$(".layui-layer-content").find('iframe');
    iframeele.css({"height": $(iframeele).height() - 10});
}


Group.prototype.addgrouporg = function (groupid, orgids) {
    $.ajax({
        type: "POST",
        url: "insert_grouporg",
        data: {groupid: groupid, orgids: orgids},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                top.mylayui.msg(data.msg, {icon: 1});
                top.mylayui.closeAll('iframe');
                document.location.reload();
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}


var vOrgGroup;
var vTable;
$(function () {
    vOrgGroup = new Group();
})


function CloseDialog(result, msg) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        document.formedit.reset();
        vOrgGroup.searchtable();
        vOrgGroup.OpTab(1);
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}